package com.novellotus.iov.carvice.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.TextView;

import com.novellotus.httploadingview.LoadingView;
import com.novellotus.iov.carvice.R;
import com.novellotus.iov.carvice.adapter.FaultExpandableAdapter;
import com.novellotus.iov.carvice.bean.RxFaultTypeBean;
import com.novellotus.iov.carvice.retrofit.BaseArrayEntity;
import com.novellotus.iov.carvice.retrofit.BaseArrayObserver;
import com.novellotus.iov.carvice.retrofit.RetrofitFactory;
import com.novellotus.iov.carvice.retrofit.RxSchedulers;
import com.novellotus.iov.carvice.utils.MyUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observable;

/**
 * 选择故障类型
 */
public class FaultTypeActivity extends BaseActivity implements FaultExpandableAdapter.FaultExpandableInterface, LoadingView.OnBtnClickListener {

    @BindView(R.id.expand)
    ExpandableListView expand;
    @BindView(R.id.tv_tittle)
    TextView tv_tittle;
    @BindView(R.id.btn_right)
    Button btn_right;
    @BindView(R.id.loadingView)
    LoadingView loadingView;
    FaultExpandableAdapter faultExpandableAdapter;
    String chooseType = "";

    @Override
    protected void initContentView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_fault_type);
    }


    List<HashMap<String, String>> group = new ArrayList<>();
    List<List<HashMap<String, String>>> childArray = new ArrayList<>();

    @Override
    public void init() {
        loadingView.setLoadingModel(LoadingView.MODEL_ALERT);
        loadingView.setOnBtnClickListener(this);
        chooseType = getIntent().getExtras().getString("chooseType");
        tv_tittle.setText("故障类型");
        btn_right.setText("确定(0/10)");
        loadingView.loading();
        getFaultData();

    }

    @OnClick(R.id.img_back)
    void back() {
        finish();
    }

    @OnClick(R.id.btn_right)
    void chooseType() {
        String choose = faultExpandableAdapter.getChooseType();
        String[] keys = faultExpandableAdapter.getChooseTypeKey();
        if (choose.length() != 0) {
            Intent intent = new Intent(this, FaultTypeActivity.class);
            intent.putExtra("type", choose);
            intent.putExtra("keys", keys);
            setResult(2000, intent);
            finish();
        } else {
            MyUtils.showToast(this, "请选择故障类型!");
        }
    }

    /**
     * 获取故障类型
     */
    public void getFaultData() {
        Observable<BaseArrayEntity<RxFaultTypeBean>> observable = RetrofitFactory.getInstance().getFaultType();
        observable.compose(RxSchedulers.<BaseArrayEntity<RxFaultTypeBean>>compose())
                .subscribe(new BaseArrayObserver<RxFaultTypeBean>(this) {
                    @Override
                    public void onSuccess(List<RxFaultTypeBean> response) {
                        loadingView.success();
                        setFaultExpandableAdapter(response);
                    }

                    @Override
                    public void onFail(int code, String msg) {
                        MyUtils.showToast(FaultTypeActivity.this, msg);
                    }

                    @Override
                    public void onError(String msg) {
                        loadingView.empty(msg, true);
                    }
                });
    }

    private void setFaultExpandableAdapter(List<RxFaultTypeBean> response) {
        for (RxFaultTypeBean r : response) {
            //添加父类
            HashMap<String, String> hashMap1 = new HashMap<>();
            hashMap1.put("tittle", r.getValue());
            hashMap1.put("open", "0");
            group.add(hashMap1);
            //添加子类
            List<HashMap<String, String>> childList = new ArrayList<>();
            for (RxFaultTypeBean.ChildrensBean b : r.getChildrens()) {
                HashMap<String, String> hashMap = new HashMap<>();
                hashMap.put("tittle", b.getValue());
                hashMap.put("key", b.getKey());
                hashMap.put("check", "0");
                childList.add(hashMap);
            }
            childArray.add(childList);
        }
        faultExpandableAdapter = new FaultExpandableAdapter(group, childArray, this, this);
        expand.setAdapter(faultExpandableAdapter);
        expand.setGroupIndicator(null);
        expand.setDivider(null);
        expand.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView expandableListView, View view, int i, long l) {
                if (expandableListView.isGroupExpanded(i)) {
                    faultExpandableAdapter.changeArrow(i, "1");
                } else {
                    faultExpandableAdapter.changeArrow(i, "0");
                }
                return false;
            }
        });
        for (int i = 0; i < faultExpandableAdapter.getGroupCount(); i++) {
            expand.expandGroup(i);
        }
        faultExpandableAdapter.setChoose(chooseType);

    }

    @Override
    public void clickCheck(int count) {
        btn_right.setText("确定(" + count + "/10)");
    }

    @Override
    public void onReload() {
        loadingView.loading();
        getFaultData();
    }
}
